package pers.lt.admin.controller;


import pers.lt.admin.services.AdminServices;
import pers.lt.hibernatebeans.*;
import pers.lt.tools.StringFacs;
import pers.lt.jdbc.Page;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by liting on 2016/7/5.
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Resource
    private AdminServices adminServices;



    @RequestMapping(value = "/index.html")
    public ModelAndView main(HttpServletRequest request, HttpServletResponse response) {
        //到了后台用户管理
        ModelAndView modelAndView = new ModelAndView("/admin/index");
        return modelAndView;
    }

    //跳转新增用户页面，如果是修改，则会查询数据出来到修改信息
    @RequestMapping(value = "/register/index.html")
    public String  register(UserInfoEntity userInfoEntity, HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("userinfo",adminServices.userSelect(userInfoEntity));
        return "/admin/register";
    }
    //新增用户
    @RequestMapping(value = "/register/form.html")
    @ResponseBody
    public UserInfoEntity  registerformct(UserInfoEntity userInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        return adminServices.userSave(userInfoEntity);
    }
    //跳转用户列表
    @RequestMapping(value = "/user/delete.html")
    public void  userDelete(UserInfoEntity userInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        adminServices.userDelete(userInfoEntity);
    }
    //跳转用户列表
    @RequestMapping(value = "/role/delete.html")
    public void  roleDelete(RoleInfoEntity roleInfoEntity, HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        adminServices.roleDelete(roleInfoEntity);
    }
    //跳转用户列表
    @RequestMapping(value = "/rights/delete.html")
    public void  rightsDelete(RightsInfoEntity rightsInfoEntity, HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        adminServices.rightsDelete(rightsInfoEntity);
    }
    //跳转用户列表
    @RequestMapping(value = "/user/index.html")
    public String  userindex(UserInfoEntity userInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        request.setAttribute("userInfoEntity",userInfoEntity);
        return "/admin/userlist";
    }
    //查询用户数据
    @RequestMapping(value = "/user/list.html")
    public void  userlist(UserInfoEntity userInfoEntity ,Page page,HttpServletRequest request, HttpServletResponse response) {
        page.setPage(request.getParameter("page"));
        page.setRows(request.getParameter("rows"));
        Page repage = adminServices.userList(userInfoEntity, page);
        int li_total = repage.getPagetotal();

        JSONArray jsonArray = JSONArray.fromObject(repage.getList());
        String json = jsonArray.toString();
        String ls_result = "{\"total\":"+li_total+",\"rows\":"+json+"}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(ls_result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //跳转角色列表
    @RequestMapping(value = "/role/index.html")
    public String  roleindex(RoleInfoEntity roleInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        return "/admin/rolelist";
    }
    //跳转角色新增
    @RequestMapping(value = "/role/new.html")
    public String  roleAddindex(RoleInfoEntity roleInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("roleinfo",adminServices.roleSelect(roleInfoEntity));
        return "/admin/role";
    }
    //新增角色
    @RequestMapping(value = "/role/form.html")
    @ResponseBody
    public RoleInfoEntity  roleform(RoleInfoEntity roleInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        return adminServices.addRole(roleInfoEntity);
    }
    //查询角色数据
    @RequestMapping(value = "/role/list.html")
    public void  rolelist(RoleInfoEntity roleInfoEntity ,Page page,HttpServletRequest request, HttpServletResponse response) {
        page.setPage(request.getParameter("page"));
        page.setRows(request.getParameter("rows"));
        Page repage = adminServices.roleList(roleInfoEntity, page);
        int li_total = repage.getPagetotal();
        JSONArray jsonArray = JSONArray.fromObject(repage.getList());
        String json = jsonArray.toString();
        String ls_result = "{\"total\":"+li_total+",\"rows\":"+json+"}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(ls_result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //跳转权限列表
    @RequestMapping(value = "/rights/index.html")
    public String  rightsIndex(RoleInfoEntity roleInfoEntity,HttpServletRequest request, HttpServletResponse response) {

        return "/admin/rightslist";
    }
    //跳转角色新增
    @RequestMapping(value = "/rights/new.html")
    public String  rightsAddindex(RightsInfoEntity rightsInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        //更新记录
        if(!StringFacs.isEmpty(rightsInfoEntity.getId())) request.setAttribute("cightsinfo",adminServices.selectRights(rightsInfoEntity));
        if(!StringFacs.isEmpty(rightsInfoEntity.getPid())){
            //查出父级对象
            rightsInfoEntity.setId(rightsInfoEntity.getPid());
            request.setAttribute("prightsinfo",adminServices.selectRights(rightsInfoEntity));
        }
        return "/admin/rights";
    }
    //新增权限
    @RequestMapping(value = "/rights/form.html")
    @ResponseBody
    public RightsInfoEntity  rightsform(RightsInfoEntity rightsInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        return adminServices.addRights(rightsInfoEntity);
    }
    //查询权限数据
    @RequestMapping(value = "/rights/list.html")
    public void  rightslist(RightsInfoEntity roleInfoEntity ,HttpServletRequest request, HttpServletResponse response) {
        List repage = adminServices.rightsList(roleInfoEntity);
        JSONArray jsonArray = JSONArray.fromObject(repage);
        String json = jsonArray.toString();
       // String ls_result = "{\"total\":"+li_total+",\"rows\":"+json+"}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //新增用户角色
    @RequestMapping(value = "/userrole/form.html")
    @ResponseBody
    public List userRoleForm(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String str = request.getParameter("parm");
        String userid = request.getParameter("userid");

       return  adminServices.userRoleSave(str,userid);
    }
    //查询用户角色信息
    @RequestMapping(value = "/userrole/list.html")
    public void  userroleList(RoleUserInfoEntity roleUserInfoEntity , Page page, HttpServletRequest request, HttpServletResponse response) {
        page.setPage(request.getParameter("page"));
        page.setRows(request.getParameter("rows"));
        Page repage = adminServices.userRoleList(roleUserInfoEntity, page);
        int li_total = repage.getPagetotal();

        JSONArray jsonArray = JSONArray.fromObject(repage.getList());
        String json = jsonArray.toString();
        String ls_result = "{\"total\":"+li_total+",\"rows\":"+json+"}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(ls_result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //新增角色对应的权限
    @RequestMapping(value = "/rolerights/form.html")
    @ResponseBody
    public List roleRightsForm(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String str = request.getParameter("parm");
        String roleId = request.getParameter("roleId");
        return adminServices.roleRightsSave(str, roleId);
    }
    //查询角色权限信息
    @RequestMapping(value = "/rolerights/list.html")
    public void  roleRightsList(RoleRightsInfoDto roleRightsInfoDto, HttpServletRequest request, HttpServletResponse response) {
        List list = adminServices.roleRightsList(roleRightsInfoDto);
        JSONArray jsonArray = JSONArray.fromObject(list);
        String json = jsonArray.toString();
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
}
